package com.vf.admin.client.controller.vivo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>Title : vivo推流</p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/8/22 11:05
 */
@Service
public class VivoPushService {

    //
    public JSONObject sendInfoMsg(String userId, String cvType, String payAmount) throws IOException {

        //生成随机32位
        String zfcAll = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        char[] zfc = zfcAll.toCharArray();

        String url = "https://ocpx.mktad.cn/openapi/v1/advertiser/behavior/upload";
        String advertiserId = "330795552137607a9d37";
        String nonce = RandomStringUtils.random(32, zfc);//32位随机码
        String accessToken = "c8398cda0cf607dd020339dae07100d09e3d362ca96db521cea3a7c23f008b4a";
        String timestamp = String.valueOf(System.currentTimeMillis());//精确到ms
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        //使用请求参数的json串替换，格式如下方示例   包名 com.ptszyxx.zhiyu.app

        String cvTime = String.valueOf(System.currentTimeMillis());
        String jsonStr = null;
        if (cvType.equals("PAY")) {
            //支付
            jsonStr = "{\"dataList\":[{\"cvTime\": \"" + cvTime + " \",\"cvType\":\"" + cvType + "\",\"userId\":\"" + userId + "\",\"userIdType\":\"OTHER\"},{\"cvTime\": \" " + cvTime + " \",\"cvType\":\"PAY_ONETIME\",\"userId\":\" " + userId + " \",\"userIdType\":\"OTHER\",\"extParam\":{\"payAmount\":\"" + payAmount + "\"}}],\"pkgName\":\"com.ptszyxx.zhiyu.app\",\"srcId\":\"ds-202308214593\",\"srcType\":\"app\"}";


        } else {
            //other
            jsonStr = "{" +
                    "\"dataList\": [" +
                    "{\"cvTime\": \"" + cvTime + "\"," +
                    "\"cvType\": \"" + cvType + "\"," +
                    "\"userId\": \"" + userId + "\"," +
                    "\"userIdType\": \"OTHER\" }" +
                    "]," +
                    "\"pkgName\": \"com.ptszyxx.zhiyu.app\"," +
                    "\"srcId\": \"ds-202308214593\"," +
                    "\"srcType\": \"app\"}";
            System.out.println("jsonStr: " + jsonStr);
        }

        RequestBody body = RequestBody.create(mediaType, jsonStr);
        Request request = new Request.Builder()
                .url(url + "?access_token=" + accessToken +
                        "&timestamp=" + timestamp +
                        "&nonce=" + nonce +
                        "&advertiser_id=" + advertiserId
                )
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        Response response = client.newCall(request).execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        return jsonObject;
    }


}
